home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2002 November / SGI IRIX 6.5 Applications 2002 November.iso / dist / gateway.idb / usr / WebFace / Source / 20-NetworkServices / routing / static.frm.z / static.frm
Encoding:
Text File  |  2002-06-12  |  9.1 KB  |  350 lines

  1. #!/usr/bin/perl5
  2. #
  3. # static.cgi
  4. #
  5. # Copyright 1988-1996 Silicon Graphics, Inc.
  6. # All rights reserved.
  7. #
  8. # This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  9. # the contents of this file may not be disclosed to third parties, copied or
  10. # duplicated in any form, in whole or in part, without the prior written
  11. # permission of Silicon Graphics, Inc.
  12. #
  13. # RESTRICTED RIGHTS LEGEND:
  14. # Use, duplication or disclosure by the Government is subject to restrictions
  15. # as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  16. # and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  17. # successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  18. # rights reserved under the Copyright Laws of the United States.
  19. #
  20. # $Id: static.frm,v 1.11 1998/02/14 01:55:12 shotes Exp $
  21.  
  22. require "/usr/OnRamp/lib/OnRamp.pm";
  23. require "/usr/OnRamp/lib/java.pm";
  24.  
  25. $conf = "/etc/config/static-route.options";
  26. $dummy = "/tmp/static.dummy";
  27. $myname = "static.cgi";
  28. $help_page = "static-help.html";
  29. $title = "Static Routing";
  30.  
  31. $js_main =
  32. "type = \"route\";
  33. $js_account_main
  34. $js_error_box
  35. $js_help
  36. $js_ip
  37. function testAdd(form) {
  38.     if (!testRoute(form.destination)) return (false);
  39.     return (true);
  40. }
  41. function testRoute(Ctrl) {
  42.     if (Ctrl.value == \"\") { 
  43.         errorBox (Ctrl, \"To add a new route, first enter \\na \"
  44.                 + \"destination IP address.\"); 
  45.         return (false); 
  46.     }
  47.     if (Ctrl.value == \"default\") return (true);
  48.     if (!testIPaddress(Ctrl.value,false)) { 
  49.         errorBox (Ctrl, \"The destination address \" + Ctrl.value 
  50.                 + \"\\nis invalid.\"); 
  51.         return (false); 
  52.     }
  53.     return (true);
  54. }";
  55.  
  56. $js_add =
  57. "$js_standard
  58. $js_error_box
  59. $js_help
  60. $js_ip
  61. function checkForm(form)  {
  62.     if (!testIP(form.route)) return (false);
  63.     return (true);
  64. }
  65. function testIP(Ctrl) {
  66.     if (Ctrl.value == \"\") { 
  67.         errorBox (Ctrl, \"The IP address of the router \\nis required.\"); 
  68.         return (false); 
  69.     }
  70.     if (!testIPaddress(Ctrl.value,false)) { 
  71.         errorBox (Ctrl, \"The router address \" + Ctrl.value 
  72.                 + \"\\nis invalid.\"); 
  73.         return (false); 
  74.     }
  75.     return (true);
  76. }";
  77.  
  78. $js_edit =
  79. "$js_standard
  80. $js_error_box
  81. $js_help
  82. $js_ip
  83. function checkForm(form) {
  84.     if (!testIP(form.route)) return (false);
  85.     return (true);
  86. }
  87. function testIP(Ctrl) {
  88.     if (Ctrl.value == \"\") { 
  89.         errorBox (Ctrl, \"The IP address of the router \\nis required.\"); 
  90.         return (false); 
  91.     }
  92.     if (!testIPaddress(Ctrl.value,false)) { 
  93.         errorBox (Ctrl, \"The router address \" + Ctrl.value 
  94.                 + \"\\nis invalid.\"); 
  95.         return (false); 
  96.     }
  97.     return (true);
  98. }";
  99.  
  100. print "Content-type: text/html\n\n";
  101.  
  102. &get_fields;
  103. &getGateways;
  104.  
  105. $delete = 0;
  106.  
  107. if (%fld) {
  108.     $help = $document_root . $ENV{"SCRIPT_NAME"};
  109.     $help =~ s/cgi$/hlp/;
  110.     exec $help if ($fld{'help'} eq "Help");
  111.  
  112.     $fld{'chosen'} =~ /([\w.]+)/;
  113.     $fld{'chosen'} = $1;
  114.     if ($fld{"add"}) { 
  115.         &validAdd;
  116.         $val{'metric'} = 1;
  117.         &getAdd;
  118.         exit 0; 
  119.     }
  120.     elsif ($fld{"doAdd"}) {
  121.         &valid(0);
  122.         &doAdd;
  123.         $fld{'destination'} = "";
  124.         &getGateways;
  125.     }
  126.     elsif ($fld{"doEdit"}) {
  127.         &valid(1);
  128.         &doEdit;
  129.         &getGateways;
  130.     }
  131.     elsif ($fld{"edit"}) {
  132.         &error(2,"To edit an existing route, first choose one from the list,
  133.             then click the edit button.") if !$fld{'chosen'};
  134.         &getGateways($fld{'chosen'});
  135.     $val{'old_route'} = $rout;
  136.     $val{'old_type'} = $type;
  137.         &putEdit;
  138.         &getEdit;
  139.         exit 0; 
  140.     }
  141.     elsif ($fld{"delete"}) { 
  142.         &error(2,"To delete an existing route, first choose one from the 
  143.             list, then click the delete button.") if !$fld{'chosen'};
  144.         $delete = 1;
  145.         $message = qq|Click "Ok" to save changes.|; 
  146.     }
  147.     elsif ($fld{"doit"}) { 
  148.         if ($fld{'delGateway'}) { &doDelete; };
  149.         &getGateways;
  150.     }
  151.     else {
  152.         $message = "Use buttons to submit form.";
  153.     }
  154.  
  155.     if (!$message) { $message = "No change made."; }
  156.     %val = %fld; 
  157.  
  158. &generic;
  159.  
  160. sub valid {
  161.     &error($_[0],"Invalid route address.") if !$fld{'route'} || &check_ipaddr($fld{'route'});
  162.  
  163. sub validAdd {
  164.     &error(2,"You must provide a route address.") if !$fld{'destination'};
  165.     if ($fld{'destination'} ne "default") {
  166.         &error(2,"Invalid route address.") 
  167.         if &check_ipaddr($fld{'destination'});   
  168.     }
  169. }
  170.  
  171. sub error {
  172.     &error_block($_[1]);
  173.     %val = %fld;
  174.     if ($_[0] == 0) { &getAdd; }
  175.     elsif ($_[0] == 1) { &getEdit; }
  176.     else { &generic; }
  177.     exit 0;
  178. }
  179.  
  180. sub doDelete {
  181.     open(IN,"< $conf");
  182.     open(OUT,"> $dummy");
  183.     while(<IN>) {
  184.         @items = split(/\s+/);
  185.         if ($items[3] eq $fld{'delGateway'}) { 
  186.         $cmd = $_;
  187.         chop $cmd;
  188.         $cmd =~ s/add/delete/;
  189.         system("$cmd > /dev/null 2>&1");
  190.         print OUT "# $_"; 
  191.     }
  192.         else { print OUT $_; }
  193.     }
  194.     close(IN);
  195.     close(OUT);
  196.     rename($dummy,$conf);
  197.  
  198.     $message .= "Route deleted.";
  199. }
  200.  
  201. sub doAdd {
  202.     $cmd = "/usr/etc/route add -$fld{'type'} $fld{'destination'} $fld{'route'}";
  203.     system("$cmd > /dev/null 2>&1");
  204.  
  205.     open(OUT,">> $conf");
  206.     print OUT "$cmd\n";
  207.     close(OUT);
  208.  
  209.     $message = "Route added.";
  210. }
  211.  
  212. sub putEdit {
  213.     $val{'type'} = $type;
  214.     $val{'route'} = $rout;
  215. }
  216.  
  217. sub doEdit {
  218.     $cmd2 = "/usr/etc/route delete -$fld{'old_type'} $fld{'chosen'} $fld{'old_route'}";
  219.     system("$cmd2 > /dev/null 2>&1");
  220.  
  221.     $cmd1 = "/usr/etc/route add -$fld{'type'} $fld{'chosen'} $fld{'route'}";
  222.     system("$cmd1 > /dev/null 2>&1");
  223.  
  224.     open(IN,"< $conf");
  225.     open(OUT,"> $dummy");
  226.     while(<IN>) {
  227.     @items = split(/\s+/);
  228.     if ($items[3] eq $fld{'chosen'}) { print OUT "# $_"; }
  229.     else { print OUT $_; }
  230.     }
  231.     print OUT "$cmd1\n";
  232.     close(IN);
  233.     close(OUT);
  234.     rename($dummy,$conf);
  235.  
  236.     $message .= "Route edited.";
  237. }
  238.     
  239. sub getEdit {
  240.     &js_title_block($title,$js_edit);
  241.     &header_block("Edit Route");
  242.  
  243.     print "<form name=StandardForm action=$myname method=post onSubmit=\"return runSubmit()\">\n";
  244.  
  245.     print "<input type=hidden name=chosen value=$fld{'chosen'}>\n";
  246.     print "<input type=hidden name=old_route value=$val{'old_route'}>\n";
  247.     print "<input type=hidden name=old_type value=$val{'old_type'}>\n";
  248.  
  249.     print "<center><table width=450>";
  250.  
  251.     print "<tr><th align=left>IP address of route destination:</th><td>
  252.         <tt>$fld{'chosen'}</tt></td></tr>";
  253.     print "<tr><th align=left>IP address of router:</th><td>",
  254.         &text('route',$val{'route'},16),"</td></tr>";
  255.     print "<tr><th align=left>Type of network route:</th><td>",
  256.         &select('type',$val{'type'},'net','host'),"</td></tr>";
  257.  
  258.     print "</table></center>";
  259.  
  260.     print &js_buttons('doEdit','Ok','onClick="markOK()"',
  261.         'onClick="markOther()"',
  262.         "onClick=\"do_help('$help_page'); return (false)\"");
  263.     
  264.     print '</form>';
  265. }
  266.  
  267. sub getAdd {
  268.     &js_title_block($title,$js_add);
  269.     &header_block("Add New Route");
  270.  
  271.     print "<form name=StandardForm action=$myname method=post onSubmit=\"return runSubmit()\">\n";
  272.  
  273.     print "<input type=hidden name=destination value=$fld{'destination'}>";
  274.  
  275.     print "<center><table width=450>";
  276.  
  277.     print "<tr><th align=left>IP address of route destination:</th><td>
  278.         <tt>$fld{'destination'}</tt></td></tr>";
  279.     print "<tr><th align=left>IP address of router:</th><td>",
  280.         &text('route',$val{'route'},16),"</td></tr>";
  281.     print "<tr><th align=left>Type of network route:</th><td>",
  282.         &select('type',$val{'type'},'net','host'),"</td></tr>";
  283.  
  284.     print "</table></center>";
  285.  
  286.     print &js_buttons('doAdd','Ok','onClick="markOK()"','onClick="markOther()"',
  287.         "onClick=\"do_help('$help_page'); return (false)\"");
  288.     
  289.     print '</form>';
  290. }
  291.    
  292. sub generic {
  293.     &js_title_block($title,$js_main);
  294.     &header_block($title);
  295.  
  296.     print "<i>$message</i>";
  297.  
  298.     print "<form name=AccountForm action=$myname method=post onSubmit=\"return runSubmit()\">\n";
  299.  
  300.     print "<center><table width=450>\n";
  301.  
  302.     print "<tr><th align=left>";
  303.     print qq|<input type=submit name=add value="Add New Route" onClick="markAdd()">|;
  304.     print "\n</th><td><input size=19 type=text name=destination 
  305.         value=$val{'destination'}>";
  306.     print "</td></tr>\n";
  307.  
  308.     if ($delete) { 
  309.         undef @locList;
  310.         foreach $arg (@routes) {
  311.             if ($arg ne $fld{'chosen'}) { 
  312.             push(@locList,$arg); }
  313.         }
  314.         print "<input type=hidden name=delGateway value=$fld{'chosen'}>"; 
  315.     } else { @locList = @routes; }
  316.  
  317.     print "<tr><th align=left>";
  318.     print qq|<input type=submit name=edit value="Edit Selected Route" onClick="markEdit()">|;
  319.     print "\n</th><td rowspan=2>", &choice_list(*locList,"chosen",20), "</td></tr>\n";
  320.  
  321.     print "<tr><th align=left>";
  322.     print qq|<input type=submit name=delete value="Delete Selected Route" onClick="markDelete()">|;
  323.     print "</th></tr>\n";
  324.     print "\n</table></center>\n";
  325.  
  326.     print &js_buttons('doit','Ok','onClick="markOther()"',
  327.         'onClick="markOther()"',
  328.         "onClick=\"do_help('$help_page'); return (false)\"");
  329.     
  330.     print '</form>';
  331. }
  332.  
  333. sub getGateways {
  334.     undef @routes;
  335.  
  336.     open(GATE, "< $conf");
  337.     while (<GATE>) {
  338.     @items = split(/\s+/);
  339.     if ($items[0] ne 'route' && $items[1] ne 'add') { next; }
  340.     push(@routes,$items[3]);
  341.     if ($items[3] eq $_[0]) {
  342.         $type = substr($items[2],1);
  343.         $rout = $items[4];
  344.     }
  345.     }
  346.     close(GATE);
  347. }
  348.